local skel = fk.CreateSkill {
  name = "emo__qingji",
}

Fk:loadTranslationTable{
  ["emo__qingji"] = "轻辎",
  [":emo__qingji"] = "当你需要使用基本牌时，你可将手牌数调整至二，若你因此失去牌，你使用下一张牌无距离次数限制。",

  ["#emo__qingji-discard"] = "轻辎:你可以弃置%arg张手牌，令下一张牌无距离次数限制",
  ["#emo__qingji-draw"] = "轻辎:你可以摸到2张",
  ["@@emo__qingji"] = "轻辎:无限制",

  ["$emo__qingji1"] = "",
  ["$emo__qingji2"] = "",
}

skel:addEffect("viewas", {
  anim_type = "drawcard",
  prompt = function (self, player, selected_cards, selected)
    local n = player:getHandcardNum() - 2
    if n > 0 then
      return "#emo__qingji-discard:::" .. n
    end
    return "#emo__qingji-draw"
  end,
  filter_pattern = {
    min_num = 0,
    max_num = 0,
    pattern = ".",
  },
  pattern = ".|.|.|.|.|basic",
  card_filter = function (self, player, to_select, selected)
    return #selected < (player:getHandcardNum() - 2)
  end,
  target_filter = Util.FalseFunc,
  view_as = function() return nil end,
  feasible = function(self, player, selected, selected_cards)
    return player:getHandcardNum() < 2 or (#selected_cards == (player:getHandcardNum() - 2))
  end,
  on_use = function(self, room, cardUseEvent)
    local player = cardUseEvent.from
    local cards = cardUseEvent.cards
    if #cards > 0 then
      room:setPlayerMark(player, "@@emo__qingji", 1)
      room:throwCard(cards, skel.name, player, player)
    else
      local n = 2 - player:getHandcardNum()
      if n > 0 then
        player:drawCards(n, skel.name)
      end
    end
  end,
  enabled_at_play = function(self, player)
    if player:getHandcardNum() == 2 then return false end
    for _, name in ipairs(Fk:getAllCardNames("b")) do
      local card = Fk:cloneCard(name)
      card:setVSPattern(nil, player, ".")
      if player:canUse(card) then
        return true
      end
    end
  end,
  enabled_at_response = function (self, player, response)
    if player:getHandcardNum() == 2 or response then return false end
    return table.filter(Fk:getAllCardNames("b"), function(name)
      local card = Fk:cloneCard(name)
      card:setVSPattern(nil, player, ".")
      if player:canUseOrResponseInCurrent(card) then
        return true
      end
    end)
  end,
})

skel:addEffect(fk.PreCardUse, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark("@@emo__qingji") ~= 0
  end,
  on_refresh = function (self, event, target, player, data)
    data.extraUse = true
    player.room:setPlayerMark(player, "@@emo__qingji", 0)
  end,
})

skel:addEffect("targetmod", {
  bypass_distances = function (self, player, skill, card, to)
    return player:getMark("@@emo__qingji") ~= 0 and card
  end,
  bypass_times = function (self, player, skill, scope, card, to)
    return player:getMark("@@emo__qingji") ~= 0 and card
  end,
})

return skel
